<template>
  <div class="layout-container">
    <!-- 顶部固定导航栏 -->
    <header class="fixed-header">
      <div class="nav-container">
        <div class="logo">非物质文化遗产系统</div>
        <nav class="nav">
          <router-link to="/home" class="nav-item">首页</router-link>
          <router-link to="/heritages" class="nav-item">文化遗产</router-link>
          <router-link to="/news" class="nav-item">文化资讯</router-link>
          <router-link to="/promotion" class="nav-item">非遗宣传</router-link>
          <router-link to="/activities" class="nav-item">文化活动</router-link>
          <router-link to="/feedback" class="nav-item">意见反馈</router-link>
          <router-link to="/announcements" class="nav-item">系统公告</router-link>
        </nav>
        <div class="user-info" v-if="userInfo">
          <el-dropdown>
            <span class="user-avatar">
              <img :src="userInfo.avatar || 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'" alt="用户头像">
              <span>{{ userInfo.username }}</span>
              <i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="info">个人信息</el-dropdown-item>
              </el-dropdown-menu>
              <el-dropdown-menu>
                <el-dropdown-item @click="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
    </header>

    <!-- 主内容区域 -->
    <main class="main-content">
      <router-view />
    </main>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { getUserInfo } from '../api/auth'

const router = useRouter()
const userInfo = ref(null)

// 获取用户信息
const fetchUserInfo = async () => {
  try {
    const token = localStorage.getItem('token')
    if (!token) {
      ElMessage.error('请先登录')
      router.push('/login')
      return
    }
    
    // 检查用户角色
    const userRole = localStorage.getItem('userRole')
    if (userRole === 'admin') {
      // 管理员角色，重定向到后台管理页面
      router.push('/admin/dashboard')
      return
    }
    
    const response = await getUserInfo()
    
    if (response.success === true || response.errorCode === 20000) {
      if (response.data) {
        userInfo.value = {
          id: response.data.id || '',
          username: response.data.username || '未知用户',
          avatar: response.data.avatar || 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png',
          ...response.data
        }
      }
    } else {
      ElMessage.error(response.message || '获取用户信息失败')
    }
  } catch (error) {
    console.error('获取用户信息失败:', error)
    if (error.response?.status === 401) {
      localStorage.removeItem('token')
      localStorage.removeItem('userRole')
      ElMessage.error('登录已过期，请重新登录')
      router.push('/login')
    } else {
      ElMessage.error('获取用户信息失败，请刷新页面重试')
    }
  }
}

// 退出登录
const logout = () => {
  localStorage.removeItem('token')
  ElMessage.success('已退出登录')
  router.push('/login')
}

// 个人信息
const info = () => {
  router.push('/info')
}

// 页面加载时获取用户信息
onMounted(() => {
  fetchUserInfo()
})
</script>

<style scoped>
.layout-container {
  width: 100%;
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* 固定顶部导航栏 */
.fixed-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  background-color: #1a5276;
  color: white;
  padding: 10px 0;
  z-index: 1000;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.nav-container {
  width: 90%;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.logo {
  font-size: 24px;
  font-weight: bold;
}

.nav {
  display: flex;
  gap: 20px;
}

.nav-item {
  color: white;
  text-decoration: none;
  padding: 5px 10px;
  transition: all 0.3s ease;
  border-radius: 4px;
}

.nav-item:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

.nav-item.router-link-active {
  background-color: rgba(255, 255, 255, 0.2);
  font-weight: bold;
}

.user-info {
  display: flex;
  align-items: center;
}

.user-avatar {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
}

.user-avatar img {
  width: 32px;
  height: 32px;
  border-radius: 50%;
}

/* 主内容区域 */
.main-content {
  flex: 1;
  margin-top: 60px; /* 为固定导航栏留出空间 */
  overflow-y: auto;
  background-color: #f5f5f5;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .nav-container {
    width: 95%;
  }
  
  .logo {
    font-size: 18px;
  }
  
  .nav {
    gap: 10px;
  }
  
  .nav-item {
    font-size: 14px;
    padding: 3px 8px;
  }
}
</style>